package com.june.junemplayer.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelStoreOwner;

import com.june.junemplayer.controller.GlobalViewModelController;
import com.june.junemplayer.pojo.Song;
import com.june.junemplayer.utils.MediaPlayerUtil;

import java.io.IOException;

public class MediaPlayerService extends Service {

    public static final int PREPARE_PLAYER = 0x00000000;
    public static final int START_SONG = 0x00000004;
    public static final int RESTART_SONG = 0x00000008;
    public static final int NEXT_SONG = 0x0000000c;
    public static final int FORE_SONG = 0x00000010;
    public static final int STOP_SONG = 0x00000014;

    private IBinder mServiceBinder = new MediaPlayerServiceBinder();

    private MediaPlayerUtil mMediaPlayerUtil;
    private GlobalViewModelController mGlobalViewModelController;



    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("JuneProcedure", getTag() + "----onBind");
        return mServiceBinder;
    }

    public class MediaPlayerServiceBinder extends Binder {
        public MediaPlayerService getService() {
            return MediaPlayerService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("JuneProcedure", getTag() + "----onCreate");
        //初始化MediaPlayer
        mMediaPlayerUtil = new MediaPlayerUtil();
        mGlobalViewModelController = new GlobalViewModelController((ViewModelStoreOwner) getApplication());
        mMediaPlayerUtil.init();
    }

    public boolean prepareSong(Song song) throws IOException {
        Log.d("JuneOperation", getTag() + "----prepareSong");
        Log.d("JuneOperationInfo", "Song = " + song);
        if (song == null) {
            return false;
        }
        //将歌曲添加至播放列表
        mGlobalViewModelController.addToPlayingList(0, song);
        Log.d("JuneOperationInfo",""+mGlobalViewModelController.getPlayingList());
        Log.d("JuneOperationInfo",""+mMediaPlayerUtil.getStatus());
        if(mMediaPlayerUtil.getStatus()== MediaPlayerUtil.MediaPlayStatus.STARTED) {
            mMediaPlayerUtil.pause();
            mMediaPlayerUtil.reset();
        }
        mMediaPlayerUtil.prepare(song.getPath());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mMediaPlayerUtil.start();
            }
        },50);
        return true;
    }

    public MediaPlayerUtil.MediaPlayStatus getMediaPlayerStatus(){
        return mMediaPlayerUtil.getStatus();
    }

    /**
     * 操作包括
     * START_SONG，开始播放；
     * RESTART_SONG，重新播放；
     * NEXT_SONG，下一首，FORE_SONG，上一首，这两部分需要判断播放列表中是否存在相应的歌曲，
     * 若不存在，则单曲循环
     */
    public boolean changeSongState(int order) {
        Log.d("JuneProcedure", getTag() + "----SendMission");
        switch (order) {
            case START_SONG:
                mMediaPlayerUtil.start();
                break;
            case RESTART_SONG:

                break;
            case NEXT_SONG:
                try {
                    prepareSong(mGlobalViewModelController.getNextInPlayingList());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case FORE_SONG:
                break;
            case STOP_SONG:
                mMediaPlayerUtil.pause();
//                mMediaPlayerUtil.stop();
                break;
        }
        return true;
    }

    private String getTag() {
        return "MediaPlayerService";
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("JuneProcedure", getTag() + "----onDestroy");
        mMediaPlayerUtil.onDestroy();
    }
}